package com.easy.base.api.controller;

import com.easy.base.api.model.dto.ResultBody;
import com.easy.base.api.model.dto.ResultPager;
import com.easy.base.api.model.dto.SessionUser;
import com.easy.base.api.model.dto.UserInfoDTO;
import com.easy.base.api.model.entity.UserInfoEntity;
import com.easy.base.api.service.IUserInfoService;

import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

@Api(tags = { "user" })
@RequestMapping("/api")
@RestController
public class UserController extends BaseController<UserInfoEntity, IUserInfoService> {

    @ApiOperation(nickname = "login", value = "登录")
    @ApiResponses({
            @ApiResponse(code = 403, message = "验证码不正确", response = ResultBody.class),
            @ApiResponse(code = 404, message = "账号或密码不正确", response = ResultBody.class),
            @ApiResponse(code = 405, message = "验证码过期，请刷新", response = ResultBody.class),
    })
    @PostMapping(value = "users/login", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<ResultBody<SessionUser>> login(@RequestBody UserInfoDTO model) {
        SessionUser user = service.login(model.getUsername(), model.getPassword(), model.getVerify());
        return ResponseEntity.ok().body(ResultBody.Success(true, user));
    }

    @ApiOperation(nickname = "logout", value = "退出登录")
    @DeleteMapping(value = "users/logout", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<ResultBody<Object>> logout() {
        service.logout();
        return ResponseEntity.noContent().build();
    }

    @ApiOperation(nickname = "currentUser", value = "当前会话用户")
    @ApiResponses({
            @ApiResponse(code = 401, message = "会话已过期", response = ResultBody.class),
    })
    @GetMapping(value = "users/current", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<ResultBody<SessionUser>> currentUser() {
        SessionUser user = service.currentUser();
        return ResponseEntity.ok().body(ResultBody.Success(true, user));
    }

    @ApiOperation(nickname = "insertUserInfo", value = "新增用户")
    @ApiResponses({
            @ApiResponse(code = 405, message = "请求参数不能为空", response = ResultBody.class),
    })
    @PostMapping(value = "users", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<ResultBody<UserInfoEntity>> insertEntity(@RequestBody UserInfoEntity model) {
        return super.insertEntity(model);
    }

    @ApiOperation(nickname = "updateUserInfo", value = "更新用户")
    @ApiResponses({
            @ApiResponse(code = 404, message = "所更新的数据不存在", response = ResultBody.class),
            @ApiResponse(code = 405, message = "ID不能为空", response = ResultBody.class),
    })
    @PutMapping(value = "users/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<ResultBody<UserInfoEntity>> updateEntity(@PathVariable("id") String id,
            @RequestBody UserInfoEntity model) {
        return super.updateEntity(id, model);
    }

    @ApiOperation(nickname = "removeUserInfo", value = "删除用户")
    @ApiResponses({
            @ApiResponse(code = 405, message = "ID不能为空", response = ResultBody.class),
    })
    @DeleteMapping(value = "users/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<ResultBody<String>> removeEntity(@PathVariable("id") String id) {
        return super.removeEntity(id);
    }

    @ApiOperation(nickname = "selectUserInfo", value = "查找指定账号")
    @ApiResponses({
            @ApiResponse(code = 405, message = "ID不能为空", response = ResultBody.class),
    })
    @GetMapping(value = "users/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<ResultBody<UserInfoEntity>> selectEntity(@PathVariable("id") String id) {
        return super.selectEntity(id);
    }

    @ApiOperation(nickname = "selectUserInfos", value = "分页查询")
    @GetMapping(value = "users", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<ResultBody<ResultPager<UserInfoEntity>>> selectEntities(
            @RequestParam UserInfoEntity entity) {
        return super.selectEntities(entity);
    }
}
